<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getCateRecommend, getRecommend } from '../api/home'
import { viewCount, filters,vidotime } from '../hooks/filters'
import { getimage } from '../hooks/image'
import { storeToRefs } from 'pinia';
import { useCounterStore } from '../stores/home'
export default defineComponent({
  components: {},
  setup() {

    const store = useCounterStore()
    const { idarr,likearr } = storeToRefs(store)
    // 作为 action 的 increment 可以直接解构
    const { increment ,incrementfalse,unlikeFuns,likeFuns} = store;

    let router: any = useRouter();
    let banners: any = ref([])
    let active: any = ref(null)
    let RCMDList: any = ref([])
    console.log(active)
    let btn = () => {
      console.log('点击了按钮')
      router.push({ path: '/videoDes' })
    }
    //获取推荐分类数据
    let getCateRecommendFun = () => {
      getCateRecommend().then((data) => {
        console.log('获取推荐分类数据=>', data)
      })
    }
    //获取推荐列表
    let getRecommendFun = () => {
      getRecommend().then((data) => {
        console.log('推荐列表=>', data)
        RCMDList.value = data.data.children

        banners.value = RCMDList._rawValue.filter((item: any) => {
          return item.type == 'uiBanner'
        })
        RCMDList.value = RCMDList._rawValue.filter((item: any) => {
          return item.type != 'uiBanner'
        })
        console.log('banner=>', banners.value[0])
        console.log('推荐列表数组RCMDList=>', RCMDList._rawValue)
      })
    };
    //点击推荐中的视频
    let click_RCMD_video=(videoid:number,video_library_id:any) => {
      console.log(videoid);
      router.push({
        path: '/videoDes',
        query: {
          aid: videoid,
          mid:video_library_id
        }
      })
    }
    onMounted(() => {
      getCateRecommendFun() //获取首页推荐数据
      getRecommendFun() //获取腿甲列表
    })
    return {
      active,
      btn,
      banners,
      RCMDList,
      click_RCMD_video,
      viewCount,
      getimage,
      vidotime,
      likearr,
    }
  }
})
</script>


<template>
    <van-loading size="24px" v-if="RCMDList==undefined" style="text-align: center;line-height:40px;">加载中...</van-loading>

  <div class="rcmd" v-else>
    <!-- 轮播图 -->
    <div class="banners" v-if="banners[0]">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" :show-indicators="false">
        <van-swipe-item v-for="(item, index) in banners[0].children" :key="index"
          ><img :src="getimage(item.model.cover)"
        /></van-swipe-item>
      </van-swipe>
    </div>
    <!-- 推荐列表 -->
    <div class="rcmdlist" v-if="RCMDList">
      <ul>
        <div 
        v-for="(item, index) in RCMDList" :key="index"
        :class="{'smalldiv':item.type == 'uiMiddleCard',
        'bigdiv':item.type == 'uiBigCardTop',
        'uiBigCard':item.type == 'uiBigCard',
        'uiCollectionSlideOne':item.type == 'uiCollectionSlideOne'
        }"
        >
          <li @click="click_RCMD_video(item.children[0].model.resource.id,item.children[0].model.resource.video_library_id)" class="small" v-if="item.type == 'uiMiddleCard'">
            <div class="smallimg">
              <div class="video_cover">
                <!-- <img src="../assets/images/swiper1.webp" /> -->
                <img :src="getimage(item.children[0].model.resource.cover)" />
                <div class="mask">
                  <div class="mask_left">
                    <span class="iconfont icon-yanjing"></span>{{viewCount(item.children[0].model.resource.count.count_view)}}
                    <div v-if="!likearr.includes(item.children[0].model.resource.id)">
                      <span class="iconfont icon-dianzan1" ></span>
                    {{viewCount(item.children[0].model.resource.count.count_like)}}
                    </div>
                    
                    <div v-else>
                       <span class="iconfont icon-dianzan1"></span>{{viewCount(item.children[0].model.resource.count.count_like+1)}}
                    </div>
                  </div>
                  <div class="right_mask">{{vidotime(item.children[0].model.resource.duration)}}</div>
                </div>
              </div>
              <div class="video_name text-cut">{{item.children[0].model.resource.title}}</div>
              <div class="video_author">
                <div class="video_auth one-text-cut">{{item.children[0].model.resource.author.userinfo.username}}</div>
                <span class="iconfont icon-gengduo"></span>
              </div>
            </div>
          </li>
          <li class="bigimg" v-if="item.type == 'uiBigCardTop'">
            <!-- bigimg -->
            <div class="part">
              <div class="cover">
                <!-- <img src="../assets/images/swiper1.webp" alt="" /> -->
                <img :src="getimage(item.children[0].model.resource.cover)" alt="" />
              </div>
              <div class="video_name flex-space">
                <div class="name">{{item.children[0].model.resource.title}}</div>
                <div class="right">
                  <span class="iconfont icon-dianzan"></span>
                  <em>{{ item.children[0].model.resource.count.count_like }}</em>
                  <span class="iconfont icon-gengduo"></span>
                </div>
              </div>
              <div class="userinfo">
                <!-- <img src="../assets/images/swiper1.webp" alt="" /> -->
                <img :src="item.children[0].model.resource.author.userinfo.avatar" alt="" />
                <em>{{item.children[0].model.resource.author.userinfo.username}}</em>
                <span>关注</span>
              </div>
            </div>
          </li>
          <li class="adimg" v-if="item.type == 'uiBigCard'">
            <div class="ad">
              <div class="ad_user flex-space">
                <div class="ad_user_left">
                  <!-- <img src="../assets/images/swiper1.webp" alt="" /> -->
                  <img :src="getimage(item.children[0].model.resource.creator.avatar)" alt="" />
                  <span>{{ item.children[0].model.resource.creator.username }}</span>
                </div>
                <div class="ad_user_right">
                  <span class="iconfont icon-gengduo"></span>
                </div>
              </div>
              <div class="title">{{ item.children[0].model.resource.title }}</div>
              <div class="ad_image">
                <!-- <img src="../assets/images/swiper1.webp" alt="" /> -->
                <img :src="getimage(item.children[0].model.resource.cover)" alt="" />

              </div>
              <div class="word">立即查看</div>
            </div>
          </li>
          <li class="rcmduser" v-if="item.type == 'uiCollectionSlideOne'">
            <div class="rcmd_user">
              <div class="top flex-space">
                <h1>推荐创作人</h1>
                <div class="logo">查看更多<span class="iconfont icon-xiangyoujiantou"></span></div>
              </div>
              <div class="userlist">
                <ul>
                  <van-swipe 
                  class="my-swipe" 
                  :width="163" 
                  indicator-color="white" 
                  :loop="false"
                  >
                    <van-swipe-item v-for="(data,index) in item.children" :key="index">
                      <li >
                        <div class="userlogo">
                          <!-- <img src="../assets/images/swiper1.webp" alt="" />avatar -->
                          <img :src="getimage(data.model.resource.avatar)"/>
                        </div>
                        <div class="username one-text-cut">{{data.model.resource.username}}</div>
                        <div class="count">{{data.model.resource.count.count_article}}作品 . {{data.model.resource.count.count_follower}}粉丝</div>
                        <div class="count">{{data.model.resource.occupation}}</div>
                        <div class="addfollow">+ 关注</div>
                      </li>
                    </van-swipe-item>
                  </van-swipe>
                </ul>
              </div>
            </div>
          </li>
        </div>
      </ul>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.rcmd {
  padding: 0 11px;
  padding-top: 6px;
  background-color: #f7f7f7;
  .banners {
    width: 100%;
    height: 199px;
    border-radius: 10px;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
  .rcmdlist {
    // display: flex;
    // flex-wrap: wrap;
    // width: 100%;
    // justify-content: space-between;

    ul {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      & > div {
        &.smalldiv{
          width: 49%;
          &:nth-child(odd){
             margin-right: 1px;
          }
        }
        &.bigdiv{
          width: 100%;
        }
        &.uiBigCard{
          width:100%;
        }
        // display: inline-block;
        li.small {
          margin-top: 7px;
          height: 175px;
          background-color: #fff;
          border-radius: 10px;
          overflow: hidden;

          .video_cover {
            position: relative;
            img {
              width: 100%;
              height: 98px;
            }
            .mask {
              position: absolute;
              bottom: 0px;
              color: #fff;
              display: flex;
              justify-content: space-between;
              align-items: center;
              font-size: 12px;
              width: 100%;
              line-height: 28px;
              .mask_left {
                display: flex;
                transform: scale(0.9);
                margin-left: -5px;
                span {
                  font-size: 15px;
                  margin-right: 1px;
                  margin-left: 12px;
                  &:first-child {
                    font-size: 18px;
                  }
                }
              }
              .right_mask {
                margin-right: 3px;
                transform: scale(0.9);
              }
            }
          }
          .video_name {
            margin-top: 5px;
            line-height: 22px;
            height: 44px;
            color: #222222;
            font-weight: 600;
            font-size: 13px;
            padding: 0 12px;
          }

          .video_author {
            display: flex;
            justify-content: space-between;
            padding: 0 12px;
            padding-right: 6px;
            line-height: 22px;
            .video_auth {
              color: #9c9c9c;
              font-size: 12px;
              flex:1;
            }
            span {
              color: #717171;
              text-align: right;
            }
          }
        }
        li.bigimg {
          width: 100%;
          .part {
            height: 291px;
            margin-top: 7px;
            background-color: #fff;
            border-radius: 10px;

            .cover {
              img {
                height: 199px;
                width: 100%;
                border-radius: 10px;
              }
            }
            .video_name {
              line-height: 48px;
              margin: 0 11px;
              border-bottom: 1px solid #f6f6f6;
              .name {
                font-size: 15px;
                color: #181818;
                font-weight: 600;
              }
              .right {
                position: relative;
                span {
                  color: #626262;
                  &:first-child {
                    margin-right: 13px;
                  }
                }
                em {
                  color: #b3b3b3;
                  position: absolute;
                  top: -8px;
                  left: 12px;
                  font-size: 12px;
                }
              }
            }
            .userinfo {
              display: flex;
              margin: 0 11px;
              line-height: 44px;
              display: flex;
              align-items: center;
              img {
                width: 23px;
                height: 23px;
                border-radius: 50%;
                margin-right: 6px;
              }
              em {
                color: #505050;
                font-size: 12px;
              }
              span {
                color: #e43f20;
                font-size: 12px;
                margin-left: 15px;
              }
            }
          }
        }
        li.adimg {
          width: 100%;
          .ad {
            height: 312px;
            width: 100%;
            background-color: #fff;
            margin-top: 7px;
            border-radius: 10px;
            .ad_user {
              padding: 0 12px;
              padding-top: 11px;
            }
            .ad_user_left {
              display: flex;
              align-items: center;
              img {
                width: 32px;
                height: 32px;
                border-radius: 50%;
                margin-right: 6px;
              }
              span {
                font-size: 12px;
                color: #1c1c1c;
              }
            }
            .ad_user_right {
              color: #797979;
            }
            .title {
              padding: 0 12px;
              color: #0e0b0d;
              line-height: 32px;
              font-size: 14px;
              height: 32px;
              font-weight: 600;
            }
            .ad_image {
              img {
                width: 100%;
                height: 199px;
              }
            }
            .word {
              padding: 0 12px;
              font-size: 12px;
              color: #b2b2b2;
              line-height: 38px;
            }
          }
        }
        li.rcmduser {
          .rcmd_user {
            margin-top: 12px;
            .top {
              line-height: 45px;
              h1 {
                font-size: 17px;
                font-weight: 600;
              }
              .logo {
                color: #bbbbbb;
                font-size: 12px;
              }
            }
            .userlist {
              ul {
                display: flex;
                li {
                  height: 168px;
                  width: 154px;
                  background-color: #fff;
                  border-radius: 10px;
                  margin-right: 9px;
                  display: flex;
                  flex-direction: column;
                  align-items: center;
                  .userlogo {
                    margin-top: 16px;
                    img {
                      height: 42px;
                      width: 42px;
                      border-radius: 50%;
                    }
                  }
                  .username {
                    font-size: 14px;
                    margin-top: 9px;
                    text-align: center;
                    width:85%;
                    color: #101010;
                  }
                  .count {
                    color: #adadad;
                    font-size: 12px;
                    margin-top: 4px;
                    &:first-child {
                      margin-top: 8px;
                    }
                  }
                  .addfollow {
                    background-color: #e54c3c;
                    color: #fff;
                    padding: 2px 13px;
                    border-radius: 20px;
                    font-size: 13px;
                    margin-top: 10px;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>